package com.sunny.ynhz.utils;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.sunny.ynhz.R;


public class ShowUtils {
    private static ProgressDialog progressDialog;
    private static Context context1;
    private static PopupWindow popupWindow;
    private static PopupWindow popupWindow1;
    private static PopupWindow popupWindow2;
    private static PopupWindow popupWindow3;

    public static ProgressDialog inItPopwin(Context context, String message) {
        Activity activity = (Activity) context;
        progressDialog = new ProgressDialog(context,
                ProgressDialog.THEME_HOLO_DARK);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog.setMessage(message);
                progressDialog.setCanceledOnTouchOutside(false);
//		progressDialog.setCancelable(true);
                progressDialog.show();
                Display defaultDisplay = ((Activity) context).getWindowManager().getDefaultDisplay();
                int width = defaultDisplay.getWidth();
                int high = defaultDisplay.getHeight();
                LayoutParams params = progressDialog.getWindow()
                        .getAttributes();
                params.alpha = 0.8f;
                params.width = (int) (width * 0.6);
                progressDialog.getWindow().setAttributes(params);
            }
        });


        return progressDialog;

    }

    public static void closePopwindow() {
        if (progressDialog != null) {
            progressDialog.dismiss();

        }

    }

    public static ProgressDialog disPlayProgress(Context context, String message) {
        progressDialog = new ProgressDialog(context);

        progressDialog = new ProgressDialog(context,
                ProgressDialog.THEME_HOLO_DARK);
        progressDialog.setMessage(message);
        progressDialog.setCanceledOnTouchOutside(false);
//		progressDialog.setCancelable(true);
        progressDialog.show();
        Display defaultDisplay = ((Activity) context).getWindowManager().getDefaultDisplay();
        int width = defaultDisplay.getWidth();
        int high = defaultDisplay.getHeight();
        LayoutParams params = progressDialog.getWindow()
                .getAttributes();
        params.alpha = 0.8f;
        params.width = (int) (width * 0.6);
        progressDialog.getWindow().setAttributes(params);
        return progressDialog;

    }

    public static void dismissProgress() {
        if (progressDialog != null) {
            progressDialog.dismiss();

        }

    }

    private static void initpopwindowAt(PopupWindow popupWindow, View view, Window window, View attbottom) {
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        LayoutParams params = window.getAttributes();
        //	params.alpha = 0.5f;
        window.setAttributes(params);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        // 点击其他地方消失
        popupWindow.setContentView(view);

        popupWindow.showAsDropDown(attbottom);

    }

    public static ProgressDialog disPlayProgress(Context context) {
        if (progressDialog != null) {
            progressDialog.dismiss();

        }
        progressDialog = new ProgressDialog(context,
                ProgressDialog.THEME_HOLO_DARK);
        progressDialog.setMessage("请稍后");
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.setCancelable(true);
        progressDialog.show();
        Display defaultDisplay = ((Activity) context).getWindowManager().getDefaultDisplay();
        int width = defaultDisplay.getWidth();
        int high = defaultDisplay.getHeight();
        LayoutParams params = progressDialog.getWindow()
                .getAttributes();
        params.alpha = 0.8f;
        params.width = (int) (width * 0.6);
        progressDialog.getWindow().setAttributes(params);
        return progressDialog;

    }

    public static void closePopWindowPage() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();

        }
    }

    private static void initpopwindowStyle(PopupWindow popupWindow, View view, Window window, View bottom, View body) {

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        LayoutParams params = window.getAttributes();
        window.setAttributes(params);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        // 点击其他地方消失
        body.setVisibility(View.VISIBLE);
        popupWindow.setContentView(view);
        popupWindow.showAsDropDown(bottom);
    }


    public static PopupWindow intPopwindowStyle(final Window window, View view, View bottom, final View bodyView) {
        popupWindow = new PopupWindow(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                LayoutParams params = window.getAttributes();
                window.setAttributes(params);
                bodyView.setVisibility(View.GONE);
            }
        });
        initpopwindowStyle(popupWindow, view, window, bottom, bodyView);
        return popupWindow;
    }

    private static void initpopwindow(PopupWindow popupWindow, View view, Window window) {
        popupWindow.setAnimationStyle(R.style.anim_popup_dir);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        LayoutParams params = window.getAttributes();
        params.alpha = 0.5f;
        window.setAttributes(params);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        // 点击其他地方消
        popupWindow.setContentView(view);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    public static PopupWindow displayPopwindow(View view, final Window window) {
        popupWindow = new PopupWindow(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);// 初始化
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                LayoutParams attributes = window.getAttributes();
                attributes.alpha = 1f;
                window.setAttributes(attributes);

            }
        });
        initpopwindow(popupWindow, view, window);
        return popupWindow;
    }

}
